package com.javadroid.sgbd;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.javadroid.model.EnderecoURLModel;

public class EnderecoURLDao extends GenericDao{
	
	private HashMap<Integer, String> mapa;
	
	public EnderecoURLDao() {
		mapa = new HashMap<Integer, String>();
	}
	
	public String getUrlAuxDaNovela(int id_novela){
		
		String result = null;
		try {
			String sql = "SELECT url_aux FROM enderecosurl WHERE id_novela = ?";
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			pstmt.setInt(1,id_novela);
			ResultSet rs = pstmt.executeQuery();
			rs.next();
			result = rs.getString(1);
		} catch (Exception e) {
			e.printStackTrace();
			result = null;
		}
		return result;
	}
	
	
	public void updateUrlAtiva(int id_novela, String urlAtualizada){
		
		try {
			String sql = "UPDATE enderecosurl SET url = ? WHERE id_novela = ?";
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			pstmt.setString(1,urlAtualizada);
			pstmt.setInt(2,id_novela);
			pstmt.execute();
			pstmt.close();
			shutdown();			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public HashMap<Integer, String> getTodasUrlAtivas() {
		
		try {
			String sql = "SELECT * FROM enderecosurl WHERE ativo = true";
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			
			ResultSet rs = pstmt.executeQuery();
			mapa.clear();
			while(rs.next()){
				EnderecoURLModel model = new EnderecoURLModel();
				model.setAtivo(rs.getBoolean("ativo"));
				model.setEnderecoAux(rs.getString("url_aux"));
				model.setEnderecoUrl(rs.getString("url"));
				model.setId_novela(rs.getInt("id_novela"));
				mapa.put(model.getId_novela(), model.getEnderecoUrl());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			mapa.clear();
		}
			return mapa;
	}
	
	public HashMap<Integer, String> getUrlAtivaDaNovela(Integer id_novela) {
		
		try {
			String sql = "SELECT * FROM enderecosurl WHERE id_novela = ?";
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			pstmt.setInt(1,id_novela);
			ResultSet rs = pstmt.executeQuery();
			mapa.clear();
			while(rs.next()){
				EnderecoURLModel model = new EnderecoURLModel();
				model.setAtivo(rs.getBoolean("ativo"));
				model.setEnderecoAux(rs.getString("url_aux"));
				model.setEnderecoUrl(rs.getString("url"));
				model.setId_novela(rs.getInt("id_novela"));
				mapa.put(model.getId_novela(), model.getEnderecoUrl());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mapa;
	}
	
	public List<EnderecoURLModel> getLista(){
		List<EnderecoURLModel> urls = new ArrayList<EnderecoURLModel>();
		String sql = "SELECT * FROM enderecosurl";
		try {
			PreparedStatement pstmt = getConnection().prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			
			while (rs.next()) {
				EnderecoURLModel model = new EnderecoURLModel();
				model.setId(rs.getInt("id"));
				model.setAtivo(rs.getBoolean("ativo"));
				model.setEnderecoAux(rs.getString("url_aux"));
				model.setEnderecoUrl(rs.getString("url"));
				model.setId_novela(rs.getInt("id_novela"));
				urls.add(model);
			}
			rs.close();
			pstmt.close();
			return urls;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
